layui.use(['form','layer','jquery','layedit','laydate','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    form.on("submit(addCategory)",function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.post("/saveCategory",{
            id : $("input[name=id]").val(),
            name : $("input[name=categoryName]").val(),
            displayOrder : $("input[name=displayOrder]").val(),
            status : $("select[name=status]").val()
        },function(res){
            top.layer.close(index);
            top.layer.msg(res.data.info);
            layer.closeAll("iframe");
            //刷新父页面
            $(".layui-tab-item.layui-show",parent.document).find("iframe")[0].contentWindow.location.reload();
        });
        return false;
    });

    //初始化下拉选项（最好是最后来初始化）
    function initSelect(){
        $('select').each(function(index,element){
            var page = $(element).attr("data-page");
            var key = $(element).attr("data-key");
            var value = $(element).attr("data-value");
            $.get("/getDictByPageKey",{page:page,key:key}, function(result){
                if(result.msg === 'success'){
                    var items = result.data;
                    items.forEach(function(item){
                        $(element).append("<option value='" + item.value + "'>"+ item.name + "</option>")
                    });
                    if(value){
                        $(element).val(value);
                    }
                    form.render("select");
                }
            });
        });
    }
    //最后初始化下拉选项
    initSelect();
});
